﻿using Dapper;
using DDD.Core.CQRS;
using DDD.Core.Result;

namespace DDD.Application.Queries.UserQuery.GetUserById
{
    internal sealed class GetUserByIdQueryHandler : QueryHandler<GetUserByIdQuery, ApiResult<GetUserByIdVO>, GetUserByIdVO>
    {
        private readonly IDBConnectionFactory _dBConnectionFactory;

        public GetUserByIdQueryHandler(IDBConnectionFactory dBConnectionFactory) : base(dBConnectionFactory)
        {
            _dBConnectionFactory = dBConnectionFactory;
        }

        public override async Task<ApiResult<GetUserByIdVO>> Handle(GetUserByIdQuery request, CancellationToken cancellationToken)
        {
            string sql = "SELECT * FROM User WHERE Id=@id";

            using var dbConnection = _dBConnectionFactory.GetDbConnection();
            var result = await dbConnection.QueryFirstOrDefaultAsync<GetUserByIdVO>(sql, request);

            return ApiResult<GetUserByIdVO>.Success(result);
        }
    }
}
